

// vue 项目的接口 代码文件 

var express = require("express")
var router = express.Router();

var {Mv,Account,School,Xueke,Banji} = require("../utils/model")
var {aesEncrypt,keys,aesDecrypt} = require("../utils");
router.get("/api",(req,res)=>{
    res.json({
        code:2000,
        msg:"测试 vue 接口是否成功--api",
        result:null
    })
});

router.get("/findmovies",(req,res)=>{
    var limit = req.query.limit * 1 || 0;
    Mv.find({},{_id:0})
    .sort({_id:1})
    .limit(limit)
    .then(result=>{
        res.json({
            msg:"请求成功",
            code:2000,
            type:1,
            result
        })
    })
    .catch(err=>{
        console.log(err);
        res.json({
            msg:"请求失败",
            code:500,
            type:0
        })
    })
})


// 登录
router.post("/login",(req,res)=>{
    var body = req.body;
    console.log(body)
    res.json({
        msg:"登录成功",
        code:2000,
        type:1,
        body
    })
})


// 注册
router.post('/signup',(req,res)=>{
    var body = req.body;
    Account.findOne({
        mobile:body.mobile
    }).then(data=>{
        if(data){
            res.json({
                msg:"手机号码已经被注册...",
                code:200,
                result:null,
                type:0,
            })
        }else{
            body.time = new Date();
            Account.insertMany(body)
            .then(result=>{
                res.json({
                    msg:"注册成功,请立即登录",
                    code:200,
                    result:result,
                    type:1,
                })
            })
        }
    })
    .catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
   
})


// 登录
router.post("/signin",(req,res)=>{
    var body = req.body; 
    Account.findOne({
        mobile:body.mobile,
        pass:body.pass,
        rolltype:body.rolltype
    }).then(data=>{
        if(data){
            // token 登录成功会有 token 保存用户信息的 
            var token = aesEncrypt(body.mobile,keys)
            req.session.token =  token;
            res.json({
                msg:"登录成功...",
                code:200,
                result:data,
                type:1,
                token
            })
        }else{
            res.json({
                msg:"登录失败,请重新登录",
                code:200,
                type:0,
            })
        }
    }).catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
});


// 获取用户信息  
router.post("/userinfo",(req,res)=>{
    const {
        token
    }  = req.session;
    console.log('session===='+req.session.token);
    const mobile = aesDecrypt(token,keys);
    Account.findOne({
        mobile
    }).then(result=>{
        res.json({
            code:200,
            msg:"获取用户信息成功",
            type:1,
            result
        })
    }).catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})


// school 查询
router.post("/getschool",(req,res)=>{
    School.find({},{}).sort({_id:-1}).then(result=>{
        res.json({
            code:200,
            msg:"获取校区信息成功",
            type:1,
            result
        })
    }).catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})

router.post("/addschool",(req,res)=>{
    var body = req.body;
    School.insertMany(body).then(result=>{
        res.json({
            code:200,
            msg:"校区信息添加成功",
            type:1,
            result
        })
    }).catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})


router.post("/delschool",(req,res)=>{
    var {_id}  = req.body;
    School.remove({_id}).then(result=>{
        res.json({
            code:200,
            msg:"校区删除成功",
            type:1,
            result
        })
    }).catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})


// 获取学科 list
router.post("/getxueke",(req,res)=>{
    Xueke.find({},{}).sort({_id:-1}).then(result=>{
        res.json({
            code:200,
            msg:"获取学科信息成功",
            type:1,
            result
        })
    }).catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})

router.post("/addxueke",(req,res)=>{
    var body = req.body;
    Xueke.insertMany(body).then(result=>{
        res.json({
            code:200,
            msg:"学科信息添加成功",
            type:1,
            result
        })
    }).catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})


router.post("/delxueke",(req,res)=>{
    var {_id}  = req.body;
    Xueke.remove({_id}).then(result=>{
        res.json({
            code:200,
            msg:"学科删除成功",
            type:1,
            result
        })
    }).catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})


// 添加班级 
router.post("/addbanji",(req,res)=>{
    console.log("addbanji")
    var body = req.body;
    Banji.findOne({
        bjid:body.bjid
    }).then(data=>{
        if(data){
            res.json({
                code:200,
                msg:"班级已经被添加",
                type:0
            })
        }else{
            Banji.insertMany(body).then(result=>{
                res.json({
                    code:200,
                    msg:"班级信息添加成功",
                    type:1,
                    result
                })
            })
        }
    }).catch(err=>{
        console.log(err);
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        
    })
})

// 获取班级列表 
router.post("/getbanjilist",(req,res)=>{
    Banji.find({},{}).sort({sch:-1,xk:-1,num:-1}).then(result=>{
        res.json({
            code:200,
            msg:"获取班级信息成功",
            type:1,
            result
        })
    }).catch(err=>{
        console.log(err);
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})


router.post("/delbanji",(req,res)=>{
    var {_id}  = req.body;
    Banji.remove({_id}).then(result=>{
        res.json({
            code:200,
            msg:"班级删除成功",
            type:1,
            result
        })
    }).catch(err=>{
        res.json({
            msg:"服务器异常",
            code:500,
            type:0,
        })
        throw err;
    })
})
module.exports = router;